<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;


class HouseModel extends Model
{

    use SoftDelete;
    // 表名
    protected $name = 'house';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

    // 追加属性
    protected $append = [
        'type_text',
        'status_text',
        'create_time_text',
        'update_time_text'
    ];


    public static function init()
    {

        self::afterDelete(function ($model) {
            $count = (new self())->where([
                'room_id' => $model->room_id,
            ])->count();
            if ($count < 1) {
                BuildingRoomModel::update(['status' => 0], ['id' => $model->room_id]);
            }

        });
    }


    public function room()
    {
        return $this->belongsTo('app\admin\model\BuildingRoomModel', 'room_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id');
    }


    public function board()
    {
        return $this->hasMany('app\admin\model\BuildingRoomBoardModel', 'room_id', 'room_id');
    }

    public function fixed()
    {
        return $this->hasMany('app\admin\model\BuildingRoomFixedModel', 'room_id', 'room_id');
    }

    public function pt()
    {
        return $this->hasMany('app\admin\model\BuildingRoomPtModel', 'room_id', 'room_id');
    }

    public function delUser()
    {
        return $this->belongsTo(\app\common\model\User::class, 'del_uid', 'id')->bind('username');
    }

    public function getImagesAttr($value, $data)
    {
        $value = $value ?: ($data['images'] ?? []);
        $images = $value ? json_decode($value, true) : [];
        foreach ($images as $k =>$v) {
            $images[$k] = cdnurl($v);
        }
        return $images;
    }

    public function getCoverAttr($value, $data)
    {
        if ($value) {
            return cdnurl($value);
        }
        $value = $this->getImagesAttr('', $data);
        return $value ? $value[0] : '';
    }

    public function getVideoAttr($value, $data)
    {
        $value = $value ?: ($data['video'] ?? '');
        return $value ? cdnurl($value) : '';
    }

    public function setImagesAttr($value)
    {
        if (empty($value)) return null;
        if (is_string($value)) {
            $value = explode(',', $value);
        }
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }

    public function getTypeList()
    {
        return [
            1 => '居住',
            2 => '商铺',
            3 => '办公',
            4 => '厂房',
            5 => '仓库',
            6 => '建材',
            7 => '物业',
            8 => '车位',
        ];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['type'] ?? '');
        $list = $this->getTypeList();
        return $list[$value] ?? '';
    }

    public function getHouseTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['house_type'] ?? '');
        return $value;
        //$list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_HOUSE);
        //return $list[$value] ?? '';
    }

    public function getPaymentTypeTextAttr($value, $data)
    {
        return $value ?: ($data['payment_type'] ?? '');
        //$list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_PAYMENT);
        //return $list[$value] ?? '';
    }

    public function getRentTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['rent_type'] ?? '');
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_RENT);
        return $list[$value] ?? '';
    }

    public function getCommunityTextAttr($value, $data)
    {
        $value = $value ?: ($data['community'] ?? '');
        $list = ConfigValueModel::getCommunityList();
        return $list[$value] ?? '';
    }

    public function getStreetTextAttr($value, $data)
    {
        $value = $value ?: ($data['street'] ?? '');
        $pid = (new ConfigValueModel())->getIdByCidOptionId(ConfigValueModel::TYPE_COMMUNITY, $data['community']);
        $list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_STREET, $pid);
        return $list[$value] ?? '';
    }


    /*public function getFacilitiesListAttr($value, $data)
    {


        //return BuildingRoomPtModel::where('room_id', $data['room_id'] ?? 0)->column('name');

        $facilities = $value ?: ($data['facilities'] ?? '');
        if (empty($facilities)) {
            return [];
        }
        $tags_arr = (new ConfigValueModel())->where('cid', ConfigValueModel::TYPE_FACILITIES)
            ->column('option_id,option_name,icon','option_id');
        foreach ($tags_arr as $k => $v) {
            if (!empty($v['icon'])) {
                $tags_arr[$k]['icon'] = cdnurl($v['icon']);
            }
        }
        $facilities = explode(',', $facilities);
        $list = [];
        foreach ($facilities as $tag) {
            $list[] = $tags_arr[$tag] ?? '';
        }
        return array_filter($list);
    }*/

    public function getTagsTextAttr($value, $data)
    {
        $tags = $value ?: ($data['tags'] ?? []);
        return ConfigValueModel::getTagsText($tags);
    }

    public function setTagsAttr($value)
    {
        if (is_array($value)) {
            $value = implode(',', $value);
        }
        return $value;
    }

    public function getStatusList()
    {
        return ['-1' =>'待审核', '1' => '上架', 2 => '下架'];
    }

    public function getDecorationList()
    {
        return ['1' => '普通装修', 2 => '精装修'];
    }

    public function getDecorationTextAttr($value, $data)
    {
        $value = $value ?: ($data['decoration'] ?? '');
        return $this->getDecorationList()[$value] ?? '';
    }

    public function getDefaultSearchList()
    {
        return ['0' => '否', 1 => '是'];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
